<template>
  <div class="loginContainer">
    <el-card class="login-card">
     
      <div class="login-body">
        <el-form 
        :model="formdate"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          label-position="left"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
            v-model="formdate.username"
              type="text"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户密码" prop="password">
            <el-input
            v-model="formdate.password"
              type="password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirm">
            <el-input
              v-model="formdate.confirm"
              type="password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')" class="register"
              >注册</el-button
            >
            <el-button @click="resetForm" type="info" class="subscriber">已有账号</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>
 
<script>
export default{
 data() {
  
      var validatePass = ( value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = ( value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== confirm) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
             formdate: {
        username: "",
        password: "",
        confirm:""
      },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    methods:{
      resetForm(){
        this.$router.push('/Login')
      }
    }
  }
</script>

<style scoped>
.loginContainer {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #eeeeee;
}
.login-header {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 25px;
}
.login-body {
  width: 400px;
}
.el-form-item {
  margin-bottom: 22px;
}
.register{
  float: left;
}
.subscriber{
  float: right;
}
</style>